/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstround2009;

import java.util.HashSet;
import java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import static chanlib.Chan_IO.*;

/**
 * @author robit
 */
public class DecisionTree{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C

    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        return;
    }
/*
 *
(0.2 furry
  (0.81 fast
    (0.3)
    (0.2)
  )
  (0.1 fishy
    (0.3 freshwater
      (0.01)
      (0.01)
    )
    (0.1)
  )
)
 */
    static int LINE;
    static int pt;
    static StringBuilder msg;
    final static BigInteger two=new BigInteger("2");
    static HashMap<BigInteger,Double> wtree;
    static HashMap<BigInteger,String> ftree;
    static void skipWhiteSpace(){
        while(Character.isWhitespace(msg.charAt(pt))){pt++;if(LINE==28)println(format("white %f",Math.random()));}
    }
    static void skipNumeric(){
        while(Character.isDigit(msg.charAt(pt))||msg.charAt(pt)=='.'){pt++;if(LINE==28)println(format("numer %f",Math.random()));}
    }
    static void skipLowerCase(){
        while(Character.isLowerCase(msg.charAt(pt))){pt++;if(LINE==28)println(format("lower %f",Math.random()));}
    }
    static void parse(BigInteger idx){
        skipWhiteSpace();
        int st=pt;
        skipNumeric();
        wtree.put(idx, Double.parseDouble(msg.substring(st, pt)));
        skipWhiteSpace();
        if(msg.charAt(pt)==')'){pt++;return;}
        st=pt;
        skipLowerCase();
        ftree.put(idx, msg.substring(st, pt));
        skipWhiteSpace();
        if(msg.charAt(pt)=='('){pt++; parse(idx.multiply(two).add(BigInteger.ONE));}
        skipWhiteSpace();
        if(msg.charAt(pt)=='('){pt++; parse(idx.multiply(two).add(two));}
        skipWhiteSpace();
        if(msg.charAt(pt)==')'){pt++;return;}
    }

    
    /*
      calculate the answer
    */
    private static void reckon(int line){
        LINE =line;
        int L=Integer.parseInt(Line());
        //println(L);
        pt=0;
        msg=new StringBuilder();
        for(int l=0;l<L;l++){
            msg.append(Line());
        }
        int A=Int();
        String name[]=new String[A];
        int fn[]=new int[A];
        HashSet<String> feat[]=new HashSet[A];
        for(int a=0;a<A;a++){
            name[a]=String();
            fn[a]=Int();
            feat[a]=new HashSet<String>();
            for(int j=0;j<fn[a];j++)
                feat[a].add(String());
        }
        Line();


        wtree=new HashMap<BigInteger, Double>();
        ftree=new HashMap<BigInteger, String>();
        
        skipWhiteSpace();
        if(msg.charAt(pt)=='('){pt++; parse(BigInteger.ZERO);}
        //println(wtree.toString());
        //println(ftree.toString());

        double ans[] =new double[A];
        for(int a=0;a<A;a++){
            double v=1;
            BigInteger idx=BigInteger.ZERO;v*=wtree.get(idx);
            while(ftree.containsKey(idx)){
                if(feat[a].contains(ftree.get(idx)))idx=idx.multiply(two).add(BigInteger.ONE);
                else idx=idx.multiply(two).add(two);
                v*=wtree.get(idx);
                if(LINE==28)println(format("contain %d %d",a,idx));
            }
            ans[a]=v;
        }
        String output=String.format("Case #%d:",line);
        writeln(output, OUTFILE);
        for(int a=0;a<A;a++)
            writeln(format("%.7f",ans[a]), OUTFILE);

        println(line);
        //writeln(String.format("N=%d K=%d\n%s", N, K, ans),ANALFILE);
        //println(output);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        //preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="A";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        int lineNums =0;
        try {
            /*
               Init IO,plus determination of input line number
             */
            initIO(filename);
            lineNums= Integer.parseInt(Line());
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
            }
        } finally {
            
            endIO();
        }
    }

}
